package com.quwan.app.controller;

import com.quwan.anno.AccessCount;
import com.quwan.app.service.AppCircleService;
import com.quwan.app.vo.CircleDTO;
import com.quwan.app.vo.CircleNameDTO;
import com.quwan.common.BaseController;
import com.quwan.common.result.ApiResult;
import com.quwan.common.result.PageResult;
import com.quwan.entity.TCircleComment;
import com.quwan.entity.TCircleName;
import com.quwan.entity.TCircleType;
import com.quwan.manager.controller.dto.CircleCommentDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author quan
 * @date 2022-03-20 18:01
 */

@Api(tags = "app圈子")
@RestController
@RequestMapping("app/circle")
@RequiredArgsConstructor
public class AppCircleController extends BaseController {
    private final AppCircleService circleService;


    @ApiOperation("圈子类型数据")
    @PostMapping("type")
    public ApiResult<List<TCircleType>> typeList() {
        return ApiResult.success(circleService.getTypeList());

    }

    @ApiOperation("我关注的圈子")
    @PostMapping("follower")
    public ApiResult<PageResult<TCircleName>> followerCircle(HttpServletRequest request,
                                                             @RequestParam(defaultValue = "1") int pageNo,
                                                             @RequestParam(defaultValue = "10") int pageSize) {
        String mId = "";
        try {
            mId = getId(request);
        } catch (Exception e) {
        }
        if (StringUtils.isBlank(mId))
            return ApiResult.success(new PageResult<>());
        PageResult<TCircleName> ret = circleService.followerCircleList(mId, pageNo, pageSize);
        return ApiResult.success(ret);

    }

    @AccessCount
    @ApiOperation("圈子列表数据")
    @PostMapping("list")
    public ApiResult<PageResult<CircleNameDTO>> nameList(HttpServletRequest request,
                                                         @RequestParam(defaultValue = "1") int pageNo,
                                                         @RequestParam(defaultValue = "10") int pageSize,
                                                         @ApiParam("圈子类型id") @RequestParam String id) {
        String mId = "";
        try {
            mId = getId(request);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ApiResult.success(circleService.getNameList(pageNo, pageSize, id, mId));

    }

    @ApiOperation("(圈子)关注/取消")
    @PostMapping("follow/name")
    public ApiResult<String> followCircle(HttpServletRequest request,
                                          @ApiParam("圈子id") @RequestParam String id,
                                          @ApiParam("0-取消 1关注") @RequestParam Integer status) {
        if (circleService.followCircle(getId(request), id, status))
            return ApiResult.success();

        return ApiResult.failed();

    }

    @ApiOperation("(圈子内容)关注/取消")
    @PostMapping("follow/content")
    public ApiResult<String> followCircleContent(HttpServletRequest request,
                                                 @ApiParam("圈子内容id") @RequestParam String id,
                                                 @ApiParam("0-取消 1关注") @RequestParam Integer status) {
        if (circleService.followCircleContent(getId(request), id, status))
            return ApiResult.success();

        return ApiResult.failed();

    }

    @ApiOperation("(圈子内容)列表数据")
    @PostMapping("contentList")
    public ApiResult<PageResult<CircleDTO>> contentList(HttpServletRequest request,
                                                        @RequestParam(defaultValue = "1") int pageNo,
                                                        @RequestParam(defaultValue = "10") int pageSize,
                                                        @ApiParam("圈子id") @RequestParam String id,
                                                        @ApiParam("1-热门 2-最新 3-图文 4-纯文字") @RequestParam(defaultValue = "1") String type) {
        String mId = "";
        try {
            mId = getId(request);
        } catch (Exception e) {
            e.printStackTrace();
        }
        PageResult<CircleDTO> ret = circleService.contentList(mId, pageNo, pageSize, type, id);
        return ApiResult.success(ret);
    }

    @ApiOperation("发圈子")
    @PostMapping("publish/add")
    public ApiResult<String> contentList(HttpServletRequest request,
                                         @ApiParam("圈子id") @RequestParam(required = true) String id,
                                         @ApiParam("内容") @RequestParam(required = true) String content,
                                         @ApiParam("图片 多个数组类型或,分割") @RequestParam(required = false) String imgs) {

        if (circleService.publishCircle(getId(request), id, content, imgs))
            return ApiResult.success();
        return ApiResult.failed();
    }

    @ApiOperation("圈子详情")
    @PostMapping("detail")
    public ApiResult<CircleDTO> circleDetail(HttpServletRequest request,
                                             @ApiParam("圈子内容id") @RequestParam(required = true) String id) {
        String mId = "";
        try {
            mId = getId(request);
        } catch (Exception e) {
        }
        CircleDTO ret = circleService.getCircleDetail(id, mId);
        return ApiResult.success(ret);

    }

    @ApiOperation("圈子评论列表数据")
    @PostMapping("circleCommentList")
    public ApiResult<PageResult<CircleCommentDto>> circleCommentList(@RequestParam(defaultValue = "1") int pageNo,
                                                                   @RequestParam(defaultValue = "10") int pageSize,
                                                                   @ApiParam("圈子内容id") @RequestParam(required = true) String id) {

        PageResult<CircleCommentDto> ret = circleService.getCircleCommentList(pageNo, pageSize, id);
        return ApiResult.success(ret);

    }

    @ApiOperation("圈子评论")
    @PostMapping("comment/add")
    public ApiResult circleCommentList(HttpServletRequest request,
                                       @ApiParam("圈子内容id") @RequestParam(required = true) String id,
                                       @ApiParam("评论内容 不能为空") @RequestParam(required = true) String content) {

        if (StringUtils.isBlank(content) || content.trim().length() < 3)
            return ApiResult.error("内容长度不符合");
        if (circleService.circleComment(id, content, getId(request)))
            return ApiResult.success();
        return ApiResult.failed();

    }


}
